package cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorder;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorder.vo.PurPurchaseOrderPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorder.vo.PurPurchaseOrderRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purpurchaseorder.vo.PurPurchaseOrderSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purpurchaseorder.PurPurchaseOrderDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purpurchaseorderitem.PurPurchaseOrderItemDO;
import cn.iocoder.yudao.module.erp.service.purpurchaseorder.PurPurchaseOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - ERP 采购单")
@RestController
@RequestMapping("/erp/pur-purchase-order")
@Validated
public class PurPurchaseOrderController {

    @Resource
    private PurPurchaseOrderService purPurchaseOrderService;


    /**
     * 根据发票整合单编号查询采购单
     * @param checkInvoiceId
     * @return
     */
    @GetMapping("/get-by-check-invoice-id")
    public CommonResult<List<PurPurchaseOrderRespVO>> getPurPurchaseOrderByCheckInvoiceId(@RequestParam("checkInvoiceId") String checkInvoiceId) {
        return success(purPurchaseOrderService.getPurPurchaseOrderByCheckInvoiceId(checkInvoiceId));
    }



    @PostMapping("/create")
    @Operation(summary = "创建ERP 采购单")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:create')")
    public CommonResult<Long> createPurPurchaseOrder(@Valid @RequestBody PurPurchaseOrderSaveReqVO createReqVO) {
        return success(purPurchaseOrderService.createPurPurchaseOrder(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP 采购单")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:update')")
    public CommonResult<Boolean> updatePurPurchaseOrder(@Valid @RequestBody PurPurchaseOrderSaveReqVO updateReqVO) {
        purPurchaseOrderService.updatePurPurchaseOrder(updateReqVO);
        return success(true);
    }

    @PutMapping("/updateCount")
    @Operation(summary = "更新ERP 采购单")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:update')")
    public CommonResult<Boolean> updateCountPurPurchaseOrder(@Valid @RequestBody PurPurchaseOrderSaveReqVO updateReqVO) {
        purPurchaseOrderService.updateCountPurPurchaseOrder(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP 采购单")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:delete')")
    public CommonResult<Boolean> deletePurPurchaseOrder(@RequestParam("id") String id) {
        purPurchaseOrderService.deletePurPurchaseOrder(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP 采购单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:query')")
    public CommonResult<PurPurchaseOrderRespVO> getPurPurchaseOrder(@RequestParam("id") String id) {
        PurPurchaseOrderDO purPurchaseOrder = purPurchaseOrderService.getPurPurchaseOrder(id);
        return success(BeanUtils.toBean(purPurchaseOrder, PurPurchaseOrderRespVO.class));
    }

    @GetMapping("/getPurPurchaseOrderByOrderIntegrateId")
    @Operation(summary = "获得ERP 采购单")
    @Parameter(name = "orderIntegrateId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:query')")
    public CommonResult<List<PurPurchaseOrderRespVO>> getPurPurchaseOrderByOrderIntegrateId(@RequestParam("orderIntegrateId") String orderIntegrateId) {
        List<PurPurchaseOrderDO> purPurchaseOrder = purPurchaseOrderService.getPurPurchaseOrderByOrderIntegrateId(orderIntegrateId);
        return success(BeanUtils.toBean(purPurchaseOrder, PurPurchaseOrderRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP 采购单分页")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:query')")
    public CommonResult<PageResult<PurPurchaseOrderRespVO>> getPurPurchaseOrderPage(@Valid PurPurchaseOrderPageReqVO pageReqVO) {
        PageResult<PurPurchaseOrderDO> pageResult = purPurchaseOrderService.getPurPurchaseOrderPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PurPurchaseOrderRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP 采购单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPurPurchaseOrderExcel(@Valid PurPurchaseOrderPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PurPurchaseOrderDO> list = purPurchaseOrderService.getPurPurchaseOrderPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP 采购单.xls", "数据", PurPurchaseOrderRespVO.class,
                        BeanUtils.toBean(list, PurPurchaseOrderRespVO.class));
    }

    // ==================== 子表（ERP 采购单项） ====================

    @GetMapping("/pur-purchase-order-item/list-by-purchase-order-id")
    @Operation(summary = "获得ERP 采购单项列表")
    @Parameter(name = "purchaseOrderId", description = "采购单编号-提交供应商采购单编号")
    @PreAuthorize("@ss.hasPermission('erp:pur-purchase-order:query')")
    public CommonResult<List<PurPurchaseOrderItemDO>> getPurPurchaseOrderItemListByPurchaseOrderId(@RequestParam("purchaseOrderId") String purchaseOrderId) {
        return success(purPurchaseOrderService.getPurPurchaseOrderItemListByPurchaseOrderId(purchaseOrderId));
    }

}